package com.qf.lanzhou.shop.api.service.impl;

import com.qf.lanzhou.shop.api.dao.PageMapper;
import com.qf.lanzhou.shop.api.service.PageService;
import com.qf.lanzhou.shop.api.service.SearchService;
import com.qf.lanzhou.shop.pojo.entity.Goods;
import com.qf.lanzhou.shop.pojo.entity.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/**
 * @author 是柠檬甜
 * @date 2021/1/11 14:50
 */
@Service
public class PageServiceImpl implements PageService {
    @Autowired
    private PageMapper pageMapper;
    @Autowired
    private SearchService searchService;
    @Override
    public List<Goods> pageDivide(Page page) {
        List<Goods> goods = pageMapper.selectAll(page);
        return goods;
    }

    @Override
    public int count() {
        int sum = pageMapper.pageCount();
        if (sum % 5 > 0) {
            return (sum / 5) + 1;
        }
        return sum / 5;
    }

    @Override
    public List<Goods> pageSearch(Page page,String searchKey) {
        HashSet<Goods> search = searchService.search(searchKey);
        System.out.println("wuhawuhawuha"+search);
        Iterator<Goods> iterator = search.iterator();
        List list=new ArrayList();
        while (iterator.hasNext()){
            list.add(iterator.next().getId());
        }
        page.setSearchIds(list);
        System.out.println("qqqqqq"+list);
        List<Goods> goodsList = pageMapper.selectSearch(page);
        return goodsList;
    }

    @Override
    public int searchCount(String searchKey)  {
        HashSet<Goods> search = searchService.search(searchKey);
        int size = search.size();
        System.out.println("searchCount:"+size);
        if (size % 4 > 0) {
            return (size / 4) + 1;
        }
        return size / 4;
    }
}
